//
//  SSWorkContentCell.swift
//  拾舍清洁
//
//  Created by sky.wei on 2023/9/11.
//

import UIKit

class SSWorkContentCell: UITableViewCell,UITextFieldDelegate {
    
    @IBOutlet weak var selectImgV: UIImageView!
    @IBOutlet weak var imageV: UIImageView!
    
    @IBOutlet weak var nameLab: UILabel!
    
    @IBOutlet weak var textField: UITextField!
    @IBOutlet weak var addView:SSNumberButton!
    
    @IBOutlet weak var cardView: UIView!
    
    var callback:SSCallBack?
    
    override func awakeFromNib() {
        super.awakeFromNib()
        self.contentView.backgroundColor = UIColor.themeBlueColor
        
        textField.delegate = self
        textField.keyboardType = .decimalPad
        
//        addView._maxValue = 9
        //+-回调
        addView.numberResult{ (number,isAdd) in
            
            if(self.model.timeLimit == 0){
                UIViewController.current().view.makeToast("请先输入时间")
                return
            }
            
//            let result = Float(number)!
//            if(result > 0){
//                self.selectImgV.image = UIImage(named: "circle_selected")
//            }else{
//                self.selectImgV.image = UIImage(named: "circle_normal")
//            }
//            self.model.itemCount = result
//            self.addView.textField.text = number
//            (self.callback ?? {})()
            self.handleCount(number: number)
        }
        
    }
    
    func handleCount(number:String) {
        let result = Float(number)!
        if(result > 0){
            self.selectImgV.image = UIImage(named: "circle_selected")
        }else{
            self.selectImgV.image = UIImage(named: "circle_normal")
        }
        self.model.itemCount = result
        self.addView.textField.text = number
        (self.callback ?? {})()
    }
    
    func callback(_ finished:@escaping SSCallBack) {
        callback = finished
    }

    var model:SSWorkContent = SSWorkContent(){
        didSet{
            nameLab.text = model.itemName
            addView._maxValue = Int(model.maximumQuantity)
            imageV.setImage(with: model.imgUrl, placeholderImage: UIImage(named: "place_holder_icon"))
            if(model.timeLimit > 0){
                textField.isHidden = true
                textField.isUserInteractionEnabled = false
            }else{
                textField.isHidden = false
                textField.isUserInteractionEnabled = true
            }
        }
    }
    
    func textFieldDidEndEditing(_ textField: UITextField) {
        
        model.timeLimit = stringToFloat(str: textField.text ?? "")
        if(model.timeLimit == 0){
            handleCount(number: "0")
        }
        print("model.timeLimit:\(model.timeLimit)")
    }
    
    
}
